import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomishop/app/service/MyScreenExtention.dart';

import '../../../routes/app_pages.dart';
import '../controllers/goodlist_controller.dart';

class GoodlistView extends GetView<GoodlistController> {
  const GoodlistView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: controller.scaffoldGlobalKey,
      endDrawer: Drawer(child: DrawerHeader(child: Text("右边侧边栏"))),
      appBar: _appBar(),
      body: Column(
        children: [_selectTypeView(), _goodListView()],
      ),
    );
  }

  _appBar() {
    return AppBar(
      //可以给appbar的title 给一个container
      actions: [Text("")],
      title: InkWell(
          child: Container(
              width: 800.ww,
              height: 96.hh,
              alignment: Alignment.center,
              child: Text(
                  "${controller.keyword == null ? "" : controller.keyword}",
                  style: TextStyle(color: Colors.black54, fontSize: 40.ss)),
              decoration: BoxDecoration(
                  color: Colors.black12,
                  borderRadius: BorderRadius.circular(26))),
          onTap: () {
            Get.offAndToNamed(Routes.GOOD_SEARCH);
          }),
      backgroundColor: Colors.white,
      elevation: 0,
    );
  }

  _selectTypeView() {
    return Padding(
        padding: EdgeInsets.fromLTRB(0, 30.hh, 0, 0),
        child: Obx(() => Row(
              children: [
                Expanded(
                    flex: 1,
                    child: InkWell(
                        child: Align(
                            alignment: Alignment.center,
                            child: Text("综合",
                                style: TextStyle(
                                    color: controller.sort.value.contains("all")
                                        ? Colors.red
                                        : Colors.black))),
                        onTap: () {
                          controller.setCurrentSort("all");
                        })),
                Expanded(
                    flex: 1,
                    child: InkWell(
                        child: Align(
                            alignment: Alignment.center,
                            child: Text("销量",
                                style: TextStyle(
                                    color: controller.sort.value
                                            .contains("salecount")
                                        ? Colors.red
                                        : Colors.black))),
                        onTap: () {
                          controller.setCurrentSort("salecount");
                        })),
                Expanded(
                    flex: 1,
                    child: InkWell(
                        child: Align(
                            alignment: Alignment.center,
                            child: Text("价格",
                                style: TextStyle(
                                    color:
                                        controller.sort.value.contains("price")
                                            ? Colors.red
                                            : Colors.black))),
                        onTap: () {
                          controller.setCurrentSort("price");
                        })),
                Expanded(
                    flex: 1,
                    child: InkWell(
                      child:
                          Align(child: Text("筛选"), alignment: Alignment.center),
                      onTap: () {
                        print('筛选dasdasdasdsa');
                        controller.scaffoldGlobalKey.currentState
                            ?.openEndDrawer();
                      },
                    )),
              ],
            )));
  }

  _goodListView() {
    return Expanded(
        flex: 1,
        child: Container(
          padding: EdgeInsets.fromLTRB(0, 20.hh, 0, 0),
          child: Obx(() {
            // controller.goodModelItem.refresh();
            if (controller.isRefresh.value == false) {
              if (controller.goodModelItem.isNotEmpty) {
                return RefreshIndicator(
                    child: ListView.builder(
                        controller: controller.scrollController,
                        itemCount: controller.itemCount.value,
                        itemBuilder: (context, position) {
                          return _getItemView(position);
                        }),
                    onRefresh: () async {
                      controller.refreshGoodList();
                    });
              } else {
                return Center(child: Text("暂无数据"));
              }
            } else {
              return Center(child: CircularProgressIndicator());
            }
          }),
        ));
  }

  Widget _goodItem(int position) {
    String url =
        "https://xiaomi.itying.com/${controller.goodModelItem.value[position].pic}";
    url.replaceAll("\\", "/");
    return InkWell(
      child: Container(
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20.ww), color: Colors.white),
          margin: EdgeInsets.fromLTRB(20.ww, 0.hh, 20.ww, 30.hh),
          height: 300.hh,
          child: Row(
            children: [
              Container(
                margin: EdgeInsets.fromLTRB(20.ww, 0, 0, 0),
                height: 280.hh,
                width: 240.ww,
                child: Image.network(url, fit: BoxFit.cover),
              ),
              Expanded(
                  flex: 1,
                  child: Container(
                      margin: EdgeInsets.fromLTRB(30.ww, 0, 0, 0),
                      padding: EdgeInsets.fromLTRB(20.ww, 20.hh, 0, 0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                              "${controller.goodModelItem.value[position].title}",
                              style: TextStyle(fontSize: 44.ss)),
                          Text(
                              "${controller.goodModelItem.value[position].subTitle}",
                              style: TextStyle(fontSize: 32.ss)),
                          Text(
                              "￥ ${controller.goodModelItem.value[position].price}",
                              style:
                                  TextStyle(fontSize: 40.ss, color: Colors.red))
                        ],
                      )))
            ],
          )),
      onTap: () {
        var id = controller.goodModelItem.value[position].sid;
        Get.toNamed(Routes.GOODDETAIL, arguments: {"id": id});
      },
    );
  }

  Widget _getItemView(int position) {
    if (position == controller.itemCount.value - 1) {
      return Obx(() => Container(
          child: controller.loadNoData.value == true
              ? Text("我是有底线的")
              : CircularProgressIndicator(),
          height: 100.hh,
          alignment: Alignment.center));
    } else {
      return _goodItem(position);
    }
  }
}
